#include "ui_bofugui.h"
#include "bofugui.h"
#include <iostream>




/********************************************************************************************/


void BofuGui::on_pushButton_Synth_clicked()
{
    QString synth;  //Sentence
    QString filename;   //name for the output-files
    synth=ui->lineEdit_synth->text();   //fetching information
    filename=ui->lineEdit_synthfile->text(); //fetching information
    if (filename.isEmpty()){
        filename = "temp";
    }
    if ((synth.endsWith(".")) || (synth.endsWith("!")) || (synth.endsWith("?")))
    {
        //creating the XML-file
        command = "cd "+dir.absolutePath()+"; echo \""+ synth.toLocal8Bit()+ "\" | boss_client -xml > temp.xml";
        commandexec = system(command.toAscii().data());

        //creating the RAW-files
        command = "cd "+dir.absolutePath()+"; boss_server -file temp.xml";
        commandexec = system(command.toAscii().data());

        //moving the important files into the synth-folder
            command = "cd "+dir.absolutePath()+";  mv *_1.raw synth/"+filename.toUtf8().data()+".raw; mv temp.xml synth/"+filename.toUtf8().data()+".xml;";
            commandexec = system(command.toAscii().data());


        //converting raw2wav
        if (ui->checkBox_SingleWav->isChecked()){
            command = "cd "+dir.absolutePath()+"/synth; perl "+bofu.absolutePath()+"/convert/convert.pl -f "+filename.toUtf8().data()+".raw -conv r2w";
            command.append(" 2&> 1&>> ../"+korpusname+".log");
            commandexec = system(command.toAscii().data());
        }
        //converting raw2ogg
        if (ui->checkBox_SingleOgg->isChecked()){
            command = "cd "+dir.absolutePath()+"/synth; perl "+bofu.absolutePath()+"/convert/convert.pl -f "+filename.toUtf8().data()+".raw -conv r2o";
            command.append(" 2&> 1&>> ../"+korpusname+".log");
            commandexec = system(command.toAscii().data());
        }

        //cleaning up project folder
        command = "cd "+dir.absolutePath()+"; rm *.raw *.blf; cd synth; rm 1";
        scriptDone();
        commandexec = system(command.toAscii().data());


    } else {
        QMessageBox::information(this, (QString::fromUtf8("ERROR")), QString::fromUtf8("<h2>Warning!</h2>"
                                                                                       "<p>We will not continue until you added a punctuation mark ('.','!','?')!"));
    }
}

//enables the synth-button if text is not empty
void BofuGui::enableSynthButton(const QString &text)
{
    ui->pushButton_Synth->setEnabled(!text.isEmpty());
}


/********************************************************************************************/

void BofuGui::on_pushButton_multiSynth_clicked()
{
    QString filename;   //name for the output-files
    filename=ui->lineEdit_multiSynthFile->text();
    if (!filename.isEmpty()){
        //filename=ui->lineEdit_synthfile->text(); //fetching information
    } else{
        //empty filename is safed as 'temp.*', nothing to change!
        filename="temp";
    }

    QString synth;  //Sentences
    synth=ui->plainTextEdit_multisynth->toPlainText();   //fetching information
    QStringList list;
    if ((synth.endsWith(".")) || (synth.endsWith("!")) || (synth.endsWith("?"))) //check for punctuation mark
    {
        //deviding the String in sentence-strings
        QString newString;
        for (int i=0;i<synth.length();i++)
        {
            if ((synth[i]!='.')&&(synth[i]!='!')&&(synth[i]!='?')){
                newString.append(synth[i]);
            } else{
                newString.append(synth[i]);
                list.append(newString);
                newString="";
            }
        }

        //for every sentence do
        for (int i = 0; i < list.size(); i++){
            QString textinteger;
            textinteger.setNum(i);

            //creating the XML-file
            command = "cd "+dir.absolutePath()+"; echo \""+ list.at(i).toLocal8Bit().constData()+ "\" | boss_client -xml > "+filename.toUtf8().data()+textinteger+".xml";
            commandexec = system(command.toAscii().data());

            //creating the RAW-files
            command = "cd "+dir.absolutePath()+"; boss_server -file "+filename.toUtf8().data()+textinteger+".xml";
            commandexec = system(command.toAscii().data());

            //cleaning up project folder
            command = "cd "+dir.absolutePath()+"; mv "+filename.toUtf8().data()+textinteger+".xml synth/"+filename.toUtf8().data()+textinteger+".xml; rm *plain.raw *.blf; mv "+filename.toUtf8().data()+textinteger+"*.raw synth/"+filename.toUtf8().data()+textinteger+".raw";
            commandexec = system(command.toAscii().data());

            //converting raw2wav
            if (ui->checkBox_MultiWav->isChecked()){
                command = "cd "+dir.absolutePath()+"/synth; FILES="+filename.toUtf8().data()+textinteger+".raw; for f in $FILES; do echo \"Processing $f file...\"; perl "+bofu.absolutePath()+"/convert/convert.pl -f $f -conv r2w; done";
                commandexec = system(command.toAscii().data());
            }
            //converting raw2ogg
            if (ui->checkBox_MultiOgg->isChecked()){
                command = "cd "+dir.absolutePath()+"/synth; FILES="+filename.toUtf8().data()+textinteger+".raw; for f in $FILES; do echo \"Processing $f file...\"; perl "+bofu.absolutePath()+"/convert/convert.pl -f $f -conv r2o; done";
                commandexec = system(command.toAscii().data());
            }

        }
        scriptDone();
    }
    else {
        QMessageBox::information(this, (QString::fromUtf8("ERROR")), QString::fromUtf8("<h2>Warning!</h2>"
                                                                                       "<p>We will not continue until you added a punctuation mark ('.','!','?')!"));
    }
}

//enables the Multisynth-button if text is not empty
void BofuGui::enableMultiSynthButton()
{
    ui->pushButton_multiSynth->setEnabled(!ui->plainTextEdit_multisynth->toPlainText().toUtf8().isEmpty());
}

/********************************************************************************************/

//open synth-folder
void BofuGui::on_pushButton_synthfolder_clicked()
{
    openURL(QUrl( dir.path().append("/synth") ));
}
